/**
 * related to userCenter.ui
 * 
 * @Author : 2037630@163.com
 * @Timestamp : 2017-04-19
 */
var Controller = require("core/controller")
var net = require('util/net')
var log = require("util/log")
var constant = require('config/constant')
var storage = require('util/storage')
var navigate = require('util/navigate')
var server = require('config/server')
var router = require('config/router')
var notify = sm('do_Notification')
var user =require("module/userModule")
var device=require("util/device")
var storage=require("util/storage")
var headImage=ui("headImage")
var userBackground=ui("userBackground")
Controller({
	mapping : {
		"moneySumResult.text" : "SumResult",
		"userCenter_name.text" : "userName"
	},
	data : {
		"SumResult" : "",
		"userName" : "",
		"headData":""
	},
	ui:{
		headImage:"headImage",
		userBackground:"userBackground"
	},
	event : {
		'$page':{
			"refreshMoney":function(){
				this.requestData();
			},
			"refreshImage":function(){
				this.loadImage()
			},
			"loadImage":function(){
				this.loadImage()
			}
			
		},
		"repayRecord" : {
			"touch" : function() {
				navigate.redirectTo({
					view:router.user.repayRecord
				})
			}
		},
		"repairRecord":{
			"touch" : function() {
				navigate.redirectTo({
					view:router.user.repairRecord
				})
			}
		},
		"modifyPassword":{
			"touch" : function() {
				navigate.redirectTo({
					view:router.user.modifyPassword
				})
			}
		},
		"userSettings":{
			"touch" : function() {
				navigate.redirectTo({
					view:router.user.setting
				})
			}
		},
		"headImage":{
			//点击换头像
			"touch":function(){
			device.uploadImage({
				quality:100,
				iscut:true
			},this.sendImage )

			}
		}
	},
	methods :{
//		切换头像数据请求
		sendImage:function(path){
			net.send({
				server : server.organization.uploadHeadImage,
				data:{
					  "degrId": user.data.degrId,
					  "deptId":user.data.departments[0].deptId,
					  "email": user.data.email,
					  "phone": user.data.phone,
					  "realName": user.data.realName,
					  "remark": user.data.remark,
					  "sex": user.data.sex,
					  "status": user.data.status,
					  "userId": user.data.userId,
					  "userManager":user.data.userManager,
					  "userName": user.data.userName,
					  "userPhoto":path.url
						  
				},
				success : function(data) {
					log.info("success upload")
					headImage.source=data.userPhoto;
					userBackground.source=data.userPhoto;
					this.refresh("$")
					storage.setItem(constant.storage.user_photo,data.userPhoto);
					notify.toast("修改成功")
				},
				error : function(data) {
					log.info("error upload")
					notify.toast("修改失败")
				},
				scope:this
			})
		},
//		请求用户催收回款总额数据
		requestData:function(){
			net.send({
				server:server.organization.moneySumResult,
				success:function (data){
					log.info(data)
					this.data.SumResult=data.moneySumResult;
					this.refresh('$');
					
				},
				error:function(data){
					log.info("error")
				},
				scope:this
			})
		},
//		用户信息查询
		queryUserInfo:function(){
			net.send({
				server:server.organization.userQuery,
				data:{
					deptCode:user.data.departments[0].deptCode,
					realName:user.data.realName
				},
				success:function (data){
//					是否载入用户头像数据缺省值
					if(data.content[0]==null){
						return
					}
					if(data.content[0].userPhoto!=null){
						userBackground.source=data.content[0].userPhoto
						headImage.source=data.content[0].userPhoto
					}else{
						userBackground.source="source://image/app/logo.png";
						headImage.source="source://image/app/logo.png";
					}
					
				},
				error:function(data){
					log.info("error")
				},
				scope:this
			})
		},
		loadImage:function(){
			var cacheData=storage.getItem(constant.storage.user_photo)
//			有本地缓存时加载本地缓存用户头像
			if(cacheData!=undefined){
				log.info("storage")
				userBackground.source=storage.getItem(constant.storage.user_photo)
				headImage.source=storage.getItem(constant.storage.user_photo)
			}
//			无本地缓存时进行用户信息查询
			else{
				log.info("query")
				this.queryUserInfo()
			}
		}
		
	}, 
	loaded:function(){
//		页面载入时加载用户头像
		this.loadImage()
		this.refresh("$")
	    this.data.userName=user.data.userName;
//	  页面载入时请求回款金额数据  
	    this.requestData();
	}
})
